﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 图书管理系统
{
    public partial class PublicUserForm : Form
    {
        public string Uid;
        public int CurEditIndex = -1;
        private string bookname;
        private string bookid;
        private string bookstate;
        private string operation;
        private string strsql;

        public PublicUserForm()
        {
            InitializeComponent();
        }

        private void PublicUserForm_Load(object sender, EventArgs e)
        {
            BookdataGridView.Enabled = false;
            PersondataGridView.Enabled = false;
            BookNametextBox.Enabled = false;
            BookIdtextBox.Enabled = false;
            Borrowbutton.Enabled = false;
            Returnbutton.Enabled = false;
        }

        private bool SearchSql(string table, string state)
        {
            bookid = BookIdtextBox.Text;
            switch (state)
            {
                case "未借出":
                    strsql = "select * from " + table + " where state = '" + state + "'";
                    break;
                case "未归还":
                    strsql = "select * from " + table + " where endtime = '" + state + "'";
                    break;
            }
            SqlConnection con = Utils.SQLTool.GetCon();
            con.Open();
            SqlCommand cmd = new SqlCommand(strsql, con);
            cmd.CommandText = strsql;
            object obj = cmd.ExecuteScalar();
            if (obj != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void BookInfobutton_Click(object sender, EventArgs e)
        {
            BookdataGridView.Enabled = true;
            BookdataGridView.Visible = true;
            Borrowbutton.Enabled = true;
            Returnbutton.Enabled = false;
            PersondataGridView.Visible = false;
            PersondataGridView.Enabled = false;

            string strsql = "select bid '图书编号', bookname '图书名称'from BookInfo where state = '未借出'";
            SqlConnection con = Utils.SQLTool.GetCon();
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter(strsql, con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            BookdataGridView.DataSource = dt;
            con.Close();
        }

        private void Personbutton_Click(object sender, EventArgs e)
        {
            PersondataGridView.Enabled = true;
            PersondataGridView.Visible = true;
            Borrowbutton.Enabled = false;
            Returnbutton.Enabled = true;
            BookdataGridView.Visible = false;
            BookdataGridView.Enabled = false;
            string strsql = "select a.id,b.bid as '图书编号', b.bookname as '图书名称'" +
                " ,a.starttime as '借阅日期',a.endtime as '归还日期' from BorrowInfo a left join BookInfo b " +
                " on a.bid = b.bid " +
                " left join MemberInfo c " +
                " on a.uid=c.uid " +
                " where b.state = '已借出' and a.uid = '" + Uid + "'";
            SqlConnection con = Utils.SQLTool.GetCon();
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter(strsql, con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            PersondataGridView.DataSource = dt;
            con.Close();
        }

        private void Borrowbutton_Click(object sender, EventArgs e)
        {
            bookname = BookNametextBox.Text;
            bookid = BookIdtextBox.Text;
            if (SearchSql("BookInfo", "未借出"))
            {
                string strsql = "update BookInfo set state = '已借出' where bid = '" + bookid + "'";
                SqlConnection con = Utils.SQLTool.GetCon();
                con.Open();
                SqlCommand cmd = new SqlCommand(strsql, con);
                int result1 = cmd.ExecuteNonQuery();

                DateTime dt = DateTime.Now;
                string starttime = dt.ToString("yyyy-MM-dd HH:mm:ss");
                strsql = "insert into BorrowInfo(uid,bid,starttime,endtime) values('" + Uid + "','" + bookid
                    + "','" + starttime + "','未归还')";

                cmd.CommandText = strsql;
                int result2 = cmd.ExecuteNonQuery();

                if (result1 == 1 && result2 == 1)
                {
                    this.BookNametextBox.Text = "";
                    this.BookIdtextBox.Text = "";
                    MessageBox.Show("借书成功");
                }
                else
                {
                    MessageBox.Show("借书失败");
                }
            }
            else
            {
                MessageBox.Show("全部图书已借出！无法借阅。");
            }
        }

        private void Returnbutton_Click(object sender, EventArgs e)
        {
            bookname = BookNametextBox.Text;
            bookid = BookIdtextBox.Text;
            string id;
            if (SearchSql("BorrowInfo", "未归还"))
            {
                if (this.BookIdtextBox.Text != "" && this.BookNametextBox.Text != "")
                {
                    string strsql_book = "update BookInfo set state = '未借出' where bid = '" + bookid + "'";
                    SqlConnection con = Utils.SQLTool.GetCon();
                    con.Open();

                    DateTime dt = DateTime.Now;
                    string endtime = dt.ToString("yyyy-MM-dd HH:mm:ss");
                    string strsql_borrow = "update BorrowInfo set endtime = '" + endtime + "' where uid = '" + Uid + "'and endtime = '" + "未归还" + "'";
                    SqlCommand cmd_borrow = new SqlCommand(strsql_borrow, con);
                    int result_borrow = cmd_borrow.ExecuteNonQuery();

                    if (result_borrow == 1)
                    {
                        SqlCommand cmd_book = new SqlCommand(strsql_book, con);
                        int result_book = cmd_book.ExecuteNonQuery();
                        if (result_book == 1)
                        {
                            this.BookNametextBox.Text = "";
                            this.BookIdtextBox.Text = "";
                            MessageBox.Show("归还成功");
                        }
                    }
                    else
                    {
                        MessageBox.Show("归还失败");
                    }
                }
                else
                {
                    MessageBox.Show("信息为空，归还失败！");
                }
            }
            else
            {
                MessageBox.Show("未借书！无法归还。");
            }
        }

        private void BookdataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                CurEditIndex = e.RowIndex;
                BookIdtextBox.Text = BookdataGridView.Rows[e.RowIndex].Cells[0].Value.ToString();
                BookNametextBox.Text = BookdataGridView.Rows[e.RowIndex].Cells[1].Value.ToString();
            }
        }

        private void PersondataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                CurEditIndex = e.RowIndex;
                BookIdtextBox.Text = PersondataGridView.Rows[e.RowIndex].Cells[1].Value.ToString();
                BookNametextBox.Text = PersondataGridView.Rows[e.RowIndex].Cells[2].Value.ToString();
            }
        }
    }
}
